package com.gaofei.pay;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.AlipayConfig;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.FileItem;
import java.util.Base64;
import java.util.ArrayList;
import java.util.List;

public class AlipayTradeQuery {

    public static void main(String[] args) throws AlipayApiException {
        String privateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCspZuyimkZ0hG441eITVVQyeCfwV9BgTpDocDC7IG85gHqDFiORIsKyDKPH7O0GFAHFH4bE99o8vSfTsMFPqVmQ2Y2YgkR80wCkWkJ8GNA0CNF+CscxcasmFtIfJR5Dcoheu3zK9Ej7HzqndDNKKTV896AG+PtutO7tQhyEA16jT1fMGxy6mss8jh0N6jwHr2OtA7f8rlSM//nJmRLEJmw/ezPAkYXXH4HUS0CvrJUnnKMniM81t6129yTZI3jr3RHPhc8B5YRGV/HSebfJz3jZ83qzqBnniRj7KHI1inaIzDb3b4WHlxHTj/pXpf2qi7KYu82eUQ4kOhFDO4sizeBAgMBAAECggEARLYokFdb2wXrPb1lYf79PIkMGCja43QuHyqqUCnWvLqB+odtWznWaLUiwimo2L1wZFD4m4g+O0OurhSns1eqdlfOv0gnOfitwLvkpSsCJZJw53EMn4frOVftxJyobsFZOBf3yfcfZn/Be5LRvkU1zV0gqbXv8faQPD2hndvbsXEJMRPKyOoqSDpPtC5ncpGrnP/NLfaor3PfHgQEz8MZJqWe9gCrunmJCYfJeC5xvVzLOOBdZm8qlqNINkzVYIQ6tK9Bfna7mVVdmX3ERsW0VHap1Cg6gG8Ve/Nq9zkodA5jMXCqQGBY6xXBOv0hdcYCPwbwqnbGN3Jn8W4YZLndQQKBgQD5plS4L2XOLA0H4lTX9dIwFaX+LFxu4wrjIiHMydsVBDRueNiP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1xPhAtNxMPxH9HokGMklyf9a3e8gPtBy1dOv7g0yB7V5HuUR+88MgCUYjaN9Hx1LtMeP0I8YAOMSxQ4xBKOG3GPzy6rkvr68gdt1y25qKaqJWDJMSpzgbjFy2tltJGUEGx+rsA0ZWjkwIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000126621192");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        AlipayTradeQueryModel model = new AlipayTradeQueryModel();
        model.setOutTradeNo("9999");
        request.setBizModel(model);
        AlipayTradeQueryResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
    }
}